package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * 自动调用Student方法中名字含有"d"的无参方法
 */
public class Test02 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
        Class cls = Class.forName("reflect.Student");
        Object o = cls.newInstance();

        Method[] methods = cls.getDeclaredMethods();
        for (Method method : methods){
            int count = method.getParameterCount();
            if (count==0){
                if (method.getName().contains("d")
                    && method.getModifiers() != Modifier.PRIVATE){
                    System.out.println("执行方法"+method.getName()+"()");
                    method.invoke(o);
                }
            }
        }
    }
}
